import api from '@/api';

export default {
  namespace: 'price',

  state: {
    combos: [],
    freights: {
      list: [],
      pagination: {},
    },
    auditFreights: {
      list: [],
      pagination: {},
    },
    incidentals: {
      list: [],
      pagination: {},
    },
    currentFreight: {},
    currentIncidental: {},
    priceType: [],
    grades: [],
    otherFeeList: {
      list: [],
      pagination: {},
    },
    otherFeePriceType: [],
    serviceTypes: [],
    increaseRules: {
      list: [],
      pagination: {},
    },
    currentRules: {},
    reviewList: {
      list: [],
      pagination: {},
    },
    reviewedList: {
      list: [],
      pagination: {},
    },
    airQuotes: {
      list: [],
      pagination: {},
    },
    otherQuotes: {
      list: [],
      pagination: {},
    },
    currentSearchParam: {},
  },

  effects: {
    *auditOtherFee({ payload }, { call, put }) {
      return yield call(api.price.other.audit, payload);
    },
    *queryOtherFeePriceDetail({ payload }, { call, put }) {
      return yield call(api.price.other.detail, payload);
    },
    *queryOtherFeePriceType({ payload }, { call, put }) {
      let res = yield call(api.price.other.priceType, payload);
      yield put({
        type: 'saveOtherFeePriceType',
        payload: res.data,
      });
    },
    *queryOtherFeeList({ payload }, { call, put }) {
      let res = yield call(api.price.other.list, payload);
      yield put({
        type: 'saveOtherFeeList',
        payload: {
          list: res.data,
          pagination: res.pagination,
        },
      });
    },
    *queryImportResult({ payload }, { call }) {
      return yield call(api.price.queryImportResult, payload);
    },
    *saveImportResult({ payload }, { call }) {
      return yield call(api.price.saveImportResult, payload);
    },
    *removeFreightIncidentals({ payload }, { call }) {
      return yield call(api.price.removeFreightIncidentals, payload);
    },
    *createFreightIncidentals({ payload }, { call }) {
      return yield call(api.price.createFreightIncidentals, payload);
    },
    *queryUnRelationIncidentals({ payload }, { call }) {
      return yield call(api.price.queryUnRelationIncidentals, payload);
    },
    *queryRelationIncidentals({ payload }, { call }) {
      return yield call(api.price.queryRelationIncidentals, payload);
    },
    *queryCopySaleFreight({ payload }, { call }) {
      return yield call(api.price.queryCopySaleFreight, payload);
    },
    *querySameService({ payload }, { call }) {
      return yield call(api.price.querySameService, payload);
    },
    *addSaleFreightSheets({ payload }, { call }) {
      return yield call(api.price.createSaleFreightSheets, payload);
    },
    *copySaleFreight({ payload }, { call }) {
      return yield call(api.price.copySaleFreight, payload);
    },
    *queryCopyServiceFromServiceGroup({ payload }, { call }) {
      return yield call(api.price.queryCopyServiceFromServiceGroup, payload);
    },
    *querySheetByServiceId({ payload }, { call }) {
      return yield call(api.price.querySheetByServiceId, payload);
    },
    *queryServiceByGroupId({ payload }, { call }) {
      return yield call(api.price.queryServiceByGroupId, payload);
    },
    *queryServiceListById({ payload }, { call }) {
      return yield call(api.price.queryServiceListById, payload);
    },
    *copyFreight({ payload }, { call }) {
      return yield call(api.price.copyFreight, payload);
    },
    *queryFeeTypesChildrenByParentCode({ payload }, { call }) {
      return yield call(api.price.queryFeeTypesChildrenByParentCode, payload);
    },
    *fetchCombos(_, { call, put }) {
      const response = yield call(api.price.queryCombos);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveCombos',
          payload: response.data,
        });
      }
    },
    *fetchServiceType(_, { call, put }) {
      const response = yield call(api.price.queryServiceType);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveServiceType',
          payload: response.data,
        });
      }
    },
    *fetchFreights({ payload }, { call, put }) {
      const res = yield call(api.price.queryFreights, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveFreights',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
      return res;
    },
    *fetchAuditPriceList({ payload }, { call, put }) {
      const res = yield call(api.price.queryAuditPriceList, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveAuditPriceList',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
      return res;
    },
    *fetchCurrentFreight({ payload }, { call, put }) {
      const res = yield call(api.price.queryFreightById, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCurrentFreight',
          payload: res.data,
        });
      }
      return res;
    },
    *addFreight({ payload }, { call }) {
      return yield call(api.price.createFreight, payload);
    },
    *updateFreight({ payload }, { call }) {
      return yield call(api.price.updateFreight, payload);
    },
    *deleteFreights({ payload }, { call, put }) {
      return yield call(api.price.removeFreights, payload);
    },
    *queryFreightDetail({ payload }, { call, put }) {
      return yield call(api.price.queryWeightAndRegionInfo, payload);
    },
    *addFreightSheets({ payload }, { call }) {
      return yield call(api.price.createFreightSheets, payload);
    },
    *fetchFreightSheets({ payload }, { call }) {
      return yield call(api.price.queryFreightSheets, payload);
    },
    *fetchSaleSheetsByCost({ payload }, { call }) {
      return yield call(api.price.querySaleSheetsByCost, payload);
    },
    *submitAuditFreight({ payload }, { call }) {
      return yield call(api.price.submitAuditFreight, payload);
    },
    *auditFreight({ payload }, { call }) {
      return yield call(api.price.auditFreight, payload);
    },
    *fetchIncidentals({ payload }, { call, put }) {
      const res = yield call(api.price.queryIncidentals, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveIncidentals',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
      return res;
    },
    *fetchCurrentIncidental({ payload }, { call, put }) {
      const res = yield call(api.price.queryIncidentalById, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCurrentIncidental',
          payload: res.data,
        });
      }
      return res;
    },
    *addIncidental({ payload }, { call }) {
      return yield call(api.price.createIncidental, payload);
    },
    *updateIncidental({ payload }, { call }) {
      return yield call(api.price.updateIncidental, payload);
    },
    *deleteIncidentals({ payload }, { call, put }) {
      return yield call(api.price.removeIncidentals, payload);
    },
    *queryIncidentalDetail({ payload }, { call, put }) {
      return yield call(api.price.queryWeightAndRegionInfo, payload);
    },
    *addIncidentalSheets({ payload }, { call }) {
      return yield call(api.price.createIncidentalSheets, payload);
    },
    *fetchIncidentalSheets({ payload }, { call }) {
      return yield call(api.price.queryIncidentalSheets, payload);
    },
    *submitAuditIncidental({ payload }, { call }) {
      return yield call(api.price.submitAuditIncidental, payload);
    },
    *auditIncidental({ payload }, { call }) {
      return yield call(api.price.auditIncidental, payload);
    },
    *fetchDimensions(_, { call }) {
      return yield call(api.price.queryDimensions);
    },
    *aduitCostPrice({ payload }, { call }) {
      return yield call(api.price.aduitCostPrice, payload);
    },
    *fetchPriceType(_, { call, put }) {
      const response = yield call(api.price.queryPriceType);
      if (response.errorCode === 0) {
        yield put({
          type: 'queryPriceType',
          payload: response.data,
        });
      }
    },
    *fetchGrades(_, { call, put }) {
      const response = yield call(api.price.queryGrades);
      if (response.errorCode === 0) {
        yield put({
          type: 'queryGrades',
          payload: response.data,
        });
      }
    },
    *fetchOperateLog({ payload }, { call }) {
      const response = yield call(api.price.operateLog, payload);
      return response;
    },
    *fetchIncreaseRules({ payload }, { call, put }) {
      const response = yield call(api.price.increaseRules.query, payload);
      const { data: list, pagination, errorCode } = response;
      if (errorCode === 0) {
        yield put({
          type: 'saveIncreaseRules',
          payload: {
            list,
            pagination,
          },
        });
      }
      return response;
    },
    *fetchCurrentRule({ payload }, { call, put }) {
      const res = yield call(api.price.increaseRules.queryCurrent, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCurrentRule',
          payload: res.data,
        });
      }
      return res;
    },
    *addIncreaseRule({ payload }, { call }) {
      return yield call(api.price.increaseRules.create, payload);
    },
    *updateIncreaseRule({ payload }, { call }) {
      return yield call(api.price.increaseRules.update, payload);
    },
    *deleteIncreaseRule({ payload }, { call, put }) {
      return yield call(api.price.increaseRules.delete, payload);
    },
    *submitAuditRules({ payload }, { call }) {
      return yield call(api.price.increaseRules.submitCheck, payload);
    },
    *stopRule({ payload }, { call }) {
      return yield call(api.price.increaseRules.stopRule, payload);
    },
    *auditIncreaseRule({ payload }, { call }) {
      return yield call(api.price.increaseRules.auditRule, payload);
    },
    *queryIncreaseImportResult({ payload }, { call }) {
      return yield call(api.price.increaseRules.queryImportResult, payload);
    },
    *saveIncreaseImportResult({ payload }, { call }) {
      return yield call(api.price.increaseRules.saveImportResult, payload);
    },
    *fetchReviewList({ payload }, { call, put }) {
      const res = yield call(api.price.increaseRules.queryReviewedList, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveReviewList',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
      return res;
    },

    *fetchReviewedList({ payload }, { call, put }) {
      const res = yield call(api.price.increaseRules.queryReviewedList, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveReviewedList',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
      return res;
    },
    *fetchAirQuoteList({ payload }, { call, put }) {
      const res = yield call(api.price.costPricingAudit.queryAir, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveAirQuoteList',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
      return res;
    },
    *fetchOtherQuoteList({ payload }, { call, put }) {
      const res = yield call(api.price.costPricingAudit.queryOther, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveOtherQuoteList',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
      return res;
    },
    *auditPlan({ payload }, { call }) {
      return yield call(api.price.costPricingAudit.aduitPlan, payload);
    },
    *aduitMarket({ payload }, { call }) {
      return yield call(api.price.costPricingAudit.aduitMarket, payload);
    },
    *aduitFinance({ payload }, { call }) {
      return yield call(api.price.costPricingAudit.aduitFinance, payload);
    },
    *stopSalePrice({ payload }, { call }) {
      return yield call(api.price.stopSalePrice, payload);
    },
  },
  reducers: {
    saveOtherFeeList(state, action) {
      return {
        ...state,
        otherFeeList: action.payload,
      };
    },
    saveOtherFeePriceType(state, action) {
      return {
        ...state,
        otherFeePriceType: action.payload,
      };
    },
    saveCombos(state, { payload }) {
      return {
        ...state,
        combos: payload,
      };
    },
    saveServiceType(state, { payload }) {
      return {
        ...state,
        serviceTypes: payload,
      };
    },
    saveFreights(state, { payload }) {
      return {
        ...state,
        freights: payload,
      };
    },
    saveAuditPriceList(state, { payload }) {
      return {
        ...state,
        auditFreights: payload,
      };
    },
    saveCurrentFreight(state, { payload }) {
      return {
        ...state,
        currentFreight: payload,
      };
    },
    clearCurrentFreight(state) {
      return {
        ...state,
        currentFreight: {},
      };
    },
    saveIncidentals(state, { payload }) {
      return {
        ...state,
        incidentals: payload,
      };
    },
    saveCurrentIncidental(state, { payload }) {
      return {
        ...state,
        currentIncidental: payload,
      };
    },
    clearCurrentIncidental(state) {
      return {
        ...state,
        currentIncidental: {},
      };
    },
    queryPriceType(state, { payload }) {
      return {
        ...state,
        priceType: payload,
      };
    },
    queryGrades(state, { payload }) {
      return {
        ...state,
        grades: payload,
      };
    },
    saveIncreaseRules(state, { payload }) {
      return {
        ...state,
        increaseRules: payload,
      };
    },
    saveCurrentRule(state, { payload }) {
      return {
        ...state,
        currentRules: payload,
      };
    },
    clearCurrentRule(state) {
      return {
        ...state,
        currentRules: {},
      };
    },
    saveReviewList(state, { payload }) {
      return {
        ...state,
        reviewList: payload,
      };
    },
    saveReviewedList(state, { payload }) {
      return {
        ...state,
        reviewedList: payload,
      };
    },
    saveAirQuoteList(state, { payload }) {
      return {
        ...state,
        airQuotes: payload,
      };
    },
    saveOtherQuoteList(state, { payload }) {
      return {
        ...state,
        otherQuotes: payload,
      };
    },
    saveSearchParams(state, { payload }) {
      return {
        ...state,
        currentSearchParam: payload,
      };
    },
    clearSearchParams(state) {
      return {
        ...state,
        currentSearchParam: {},
      };
    },
  },
};
